# -*- coding:utf-8 -*-
__author__ = 'gxb'
import cookielib
import urllib2
from FileUtile import *
from DbUtile import *

base_url = 'http://218.6.160.238:6011'

login_url = base_url + '/Login.do'
logout_url = base_url + '/Logout.do'
report_url = base_url + '/reportServlet?action=8'

login_data = {
    '_$isLogin': 'EnterFromLogin',
    'loginID': 'wh',
    'password': '123456'
}

group_data = {
    'dadui': 1,
    'resultPage': '/reportJsp/showReport3in.jsp?raq=%2FA2_XLJLFYLL.raq'
}

post_login_data = urllib.urlencode(login_data)
post_group_data = urllib.urlencode(group_data)

# 发送头信息
headers = {
    # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    # 'Accept-Encoding': 'gzip, deflate, sdch',
    'Host': '218.6.160.238:6011',
    'Referer': 'http://218.6.160.238:6011/',
    'User-Agent': 'Mozilla/4.0(compatible; MSIE 7.0b; Windows NT 6.0)'
}
# 初始化一个CookieJar来处理Cookie
cookieJar = cookielib.CookieJar()
# 实例化一个全局opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

def login():
    # 获取cookie
    req = urllib2.Request(login_url, post_login_data, headers)
    opener.open(req)

def logout():
    req = urllib2.Request(logout_url, headers=headers)
    result = opener.open(req)


def createAllGroup():
    groupDic = {}
    groupDic[ "1"]= u"飞行一大队"
    groupDic[ "2"]= u"飞行二大队"
    groupDic[ "3"]= u"飞行三大队"
    groupDic[ "4"]= u"飞行四大队"
    groupDic[ "5"]= u"飞行五大队"
    groupDic[ "6"]= u"飞行六大队"
    groupDic[ "7"]= u"飞行七大队"
    groupDic[ "8"]= u"飞行八大队"
    groupDic[ "9"]= u"飞行九大队"
    groupDic[ "10"]= u"飞行十大队"
    groupDic[ "11"]= u"飞行十一大队"
    groupDic[ "12"]= u"飞行十二大队"
    groupDic[ "13"]= u"飞行十三大队"
    groupDic[ "14"]= u"飞行十四大队"
    groupDic[ "15"]= u"飞行十五大队"
    groupDic[ "50"]= u"直升机大队"
    groupDic[ "99"]= u"飞行技术学院"

    listGrop = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,50,99]
    return  listGrop,groupDic

#根据大队id不同返回请求头信息
def postGroupData(groupId):
    group_data = {
    'dadui': groupId,
    'resultPage': '/reportJsp/showReport3in.jsp?raq=%2FA2_XLJLFYLL.raq'
    }
    post_group_data = urllib.urlencode(group_data)
    return post_group_data

def reqGroupStuList(report_url, post_group_data):
     # 查询指定大队数据
    req = urllib2.Request(report_url, post_group_data, headers)
    result = opener.open(req)
    #取得的是一个跳转大队链接<script>标签
    location_result = result.read()
    #取得链接有效部分 dadui=4&resultPage=%2FreportJsp%2FshowReport3in.jsp%3Fraq%3D%252FA2_XLJLFYLL.raq
    get_group_url = getGroup(location_result)
    print '%s链接:%s' % (post_group_data.split('&')[0],get_group_url)

    # 读取大队信息人员列表
    result = opener.open(get_group_url)
    #注意要ignore非法字符
    get_result_group = result.read().decode('gbk', 'ignore')
    return  get_result_group

def reqMpList(get_result_group,group_id,group_name):
    stuNum = getGroupStuLength(get_result_group)
    #测试时写为7，实际要换为 stuNum
    for i in range(0,stuNum):
        name,id_card,college_name,flyTime,get_mp_url =  getName(get_result_group,i)
        # 有无训练时间的都要查看大纲链接
        get_mp_url = encodeUrl('',get_mp_url)#实际访问要加base_url
        saveMpRecord(name, id_card,college_name, group_name, group_id, get_mp_url, flyTime)

# 先清理，再入数据。
def reqTruncate():
    truncateMpRecordUpdate()
    
# 更新时用，字段相同；和原表对比，根据时间不同得到要更新的数据
def reqMpListUpdate(get_result_group,group_id,group_name):
    stuNum = getGroupStuLength(get_result_group)
    #测试时写为7，实际要换为 stuNum
    for i in range(0,stuNum):
        name,id_card,college_name,flyTime,get_mp_url =  getName(get_result_group,i)
        # 有无训练时间的都要查看大纲链接
        get_mp_url = encodeUrl('',get_mp_url)#实际访问要加base_url
        saveMpRecordUpdate(name, id_card,college_name, group_name, group_id, get_mp_url, flyTime)



def reqTpList(mpId,mpUrl):
    #实际访问要加base_url
    get_mp_url = base_url+mpUrl
    print 'mpId%d %s' % (mpId,get_mp_url)
    result = opener.open(get_mp_url)
    #大纲html
    get_result = result.read().decode('gbk', 'ignore')
    # print u'课目信息%d：%s  %s' % (i,name,getCurrentTime())
    #课目链接
    findTopicHref(get_result,mpId)

def reqTpDate(tpId,tpUrl,tpName,groupId,idCard):
    #实际访问要加base_url
    get_tp_url = base_url+tpUrl
    print 'mpId%d %s' % (tpId,get_tp_url)
    result = opener.open(get_tp_url)
    #大纲html
    get_result = result.read().decode('gbk', 'ignore')
    # print u'课目信息%d：%s  %s' % (i,name,getCurrentTime())
    #课目链接
    findDate(tpId,tpName,groupId,idCard,get_result)


